/*
 * CallyWrapper.cpp
 *
 *  Created on: Jul 8, 2009
 *      Author: lfor
 */

#include "CallyWrapper.h"
#include "Cally.h"
#include "ClassUnderTest.h"
#include "Packet.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * Multiply(char * Params);
char * Add(char * Params);


int CallyWrapper::Initialize()
{
	Cally * TheCally = Cally::GetInstance();

	// Change HERE
	TheCally->RegisterFuntion((CallyFunction)Multiply, "Multiply", "int (int A, int B)" );
	TheCally->RegisterFuntion((CallyFunction)Add, "Add", "int (int A, int B)" );

	return(0);
}

char * Multiply(char * Params)
{
	ClassUnderTest * TheClass = new ClassUnderTest();

	// Change HERE
	int Param1 = 0;
	int Param2 = 0;
	int RetVal = 0;

	// Get params
	int Len = strlen(Params);
	int ParamIdx = 1;
	int Index = 0;
	while (Index < Len)
	{
		int i = Index;
		// search for a comma
		while ((Params[i] != ',') && (i < Len))
		{
			i++;
		}

		if (i < Len)
		{
			Params[i] = 0;
		}

		switch (ParamIdx)
		{
		case 1:
			// Change HERE
			Param1 = atoi(Params+Index);

			ParamIdx++;
			break;

		case 2:
			// Change HERE
			Param2 = atoi(Params+Index);

			ParamIdx++;
			break;

		default:
			break;
		}

		Index = i+1;
	}

	// Change HERE
	// Call function
	RetVal = TheClass->Multiply(Param1, Param2);

	// Change HERE
	// return
	char RetBuff[MAX_RETURN_SIZE];
	snprintf(RetBuff, MAX_RETURN_SIZE, "%i", RetVal);

	delete(TheClass);

	return(RetBuff);
}

char * Add(char * Params)
{
	ClassUnderTest * TheClass = new ClassUnderTest();

	// Change HERE
	int Param1 = 0;
	int Param2 = 0;
	int RetVal = 0;

	// Get params
	int Len = strlen(Params);
	int ParamIdx = 1;
	int Index = 0;
	while (Index < Len)
	{
		int i = Index;
		// search for a comma
		while ((Params[i] != ',') && (i < Len))
		{
			i++;
		}

		if (i < Len)
		{
			Params[i] = 0;
		}

		switch (ParamIdx)
		{
		case 1:
			// Change HERE
			Param1 = atoi(Params+Index);

			ParamIdx++;
			break;

		case 2:
			// Change HERE
			Param2 = atoi(Params+Index);

			ParamIdx++;
			break;

		default:
			break;
		}

		Index = i+1;
	}

	// Change HERE
	// Call function
	RetVal = TheClass->Add(Param1, Param2);

	// Change HERE
	// return
	char RetBuff[MAX_RETURN_SIZE];
	snprintf(RetBuff, MAX_RETURN_SIZE, "%i", RetVal);

	delete(TheClass);

	return(RetBuff);
}

